package cn.wolfcode.p2p.controller;

import cn.wolfcode.p2p.base.exception.CustomerException;
import cn.wolfcode.p2p.business.query.BidRequestQuery;
import cn.wolfcode.p2p.business.service.IBidRequestService;
import cn.wolfcode.p2p.util.Constants;
import cn.wolfcode.p2p.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * 借款审核相关
 */
@Controller
public class BidRequestController {
    @Autowired
    private IBidRequestService bidRequestService;

    @RequestMapping("bidrequest_publishaudit_list")
    public String bidList(@ModelAttribute("qo") BidRequestQuery qo, Model model) {
        qo.setBidRequestState(Constants.BIDREQUEST_STATE_PUBLISH_AUDITING);
        model.addAttribute("pageInfo", bidRequestService.query(qo));
        return "bidrequest/publish_audit";
    }

    /**
     * 发表审核
     */
    @RequestMapping("bidrequest_publishaudit")
    @ResponseBody
    public JsonResult bidrequesPublishAudit(Long id, Integer state, String remark, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date publishTime) {
        JsonResult jsonResult = new JsonResult();
        try {
            bidRequestService.publisAudit(id, state, remark, publishTime);
        } catch (CustomerException e) {
            jsonResult.setMessage(e.getMessage());
            throw new CustomerException(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("系统繁忙中,请稍后再试");
        }
        return jsonResult;
    }

    /**
     * 满标一审
     */
    @RequestMapping("bidrequest_audit1_list")
    public String auditList1(Model model, @ModelAttribute("qo") BidRequestQuery qo){
        qo.setBidRequestState(Constants.BIDREQUEST_STATE_APPROVE_PENDING_1);
        model.addAttribute("pageInfo", bidRequestService.query(qo));
        return "bidrequest/audit1";
    }

    /**
     * 满标一审审核
     * @param id
     * @param state
     * @param remark
     * @return
     */
    @RequestMapping("bidrequest_audit1")
    @ResponseBody
    public JsonResult bidrequest_audit1(Long id, Integer state, String remark){
        JsonResult jsonResult = new JsonResult();
        try {
            bidRequestService.fullAudit1(id,  state, remark);
        } catch (CustomerException e) {
            jsonResult.setMessage(e.getMessage());
            throw new CustomerException(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("系统繁忙中,请稍后再试");
        }
        return jsonResult;
    }
    /**
     * 满标二审
     */
    @RequestMapping("bidrequest_audit2_list")
    public String auditList2(Model model, @ModelAttribute("qo") BidRequestQuery qo){
        qo.setBidRequestState(Constants.BIDREQUEST_STATE_APPROVE_PENDING_2);
        model.addAttribute("pageInfo", bidRequestService.query(qo));
        return "bidrequest/audit2";
    }

    /**
     * 满标二审审核
     * @param id
     * @param state
     * @param remark
     * @return
     */
    @RequestMapping("bidrequest_audit2")
    @ResponseBody
    public JsonResult bidrequest_audit2(Long id, Integer state, String remark){
        JsonResult jsonResult = new JsonResult();
        try {
            bidRequestService.fullAudit2(id,  state, remark);
        } catch (CustomerException e) {
            jsonResult.setMessage(e.getMessage());
            throw new CustomerException(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("系统繁忙中,请稍后再试");
        }
        return jsonResult;
    }


}
